﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

/*
 * TH07 Groep 10
 * ----------------------------------------------------------------------------------
 * Versie   Door          Commentaar
 * 0.1      Wouter        Opzet en programmeer werk
 * 0.2      Wouter        verbetere en werkend maken voor de unformiteit
 * 0.3      Wouter        Zelf uitvoerend gemaakt
 * 0.31     Luca          Static gemaakt
 *
 */

namespace THO7_Team8
{
	public static class RotatePlate
	{
		public static Bitmap run(Bitmap img)
		{
			float angle = (float) findAngle(img);
			//create a new empty bitmap to hold rotated image
			Bitmap returnBitmap = new Bitmap(img.Width, img.Height);
			//make a graphics object from the empty bitmap
			Graphics g = Graphics.FromImage(returnBitmap);
			//move rotation point to center of image
			g.TranslateTransform((float) img.Width / 2, (float) img.Height / 2);
			//rotate
			g.RotateTransform(angle);
			//move image back
			g.TranslateTransform(-(float) img.Width / 2, -(float) img.Height / 2);
			//draw passed in image onto graphics object
			g.DrawImage(img, new Point(0, 0));
			//Makes the Bitmap monochrome again to prevent the smoothing of the image
			// returnBitmap = MyImage.makeMonochrome(returnBitmap);
			return returnBitmap;
		}

		//Finds the angle that the license plate needs to rotate
		public static double findAngle(Bitmap image)
		{
			//Bitmap binairImage = THO7_Team8.Filters.Binair.RunBitmapAlgorithm(image);

			CornerCoordinates cc = new CornerCoordinates(image);
			Point[] points = cc.GetCornerCoordinates();

			Point leftTop = points[0];
			Point rightTop = points[1];
			Point rightBottom = points[2];
			Point leftBottom = points[3];

			float angle = 0;
			double radian = 0.0;
			int ylefttop = leftTop.Y;
			int yrighttop = rightTop.Y;
			int xlefttop = leftTop.X;
			int xrighttop = rightTop.X;
			int som = ylefttop - yrighttop;
			int som2 = xrighttop - xlefttop;
			radian = (double) som / (double) som2;
			angle = (float) ((radian * (180 / Math.PI)));

			return angle;
		}
	}
}